package com.education.api.controller.admin.system;


import com.education.business.service.system.SystemMenuService;
import com.education.common.base.BaseController;
import com.education.common.util.Result;
import com.education.common.util.ResultCode;
import com.education.model.dto.MenuTree;
import com.education.model.entity.SystemMenu;
import com.education.model.entity.SystemRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 余一
 * @since 2021-09-10
 */
@RestController
@RequestMapping("/system/menu")
public class SystemMenuController extends BaseController {

    @Autowired
    private SystemMenuService systemMenuService;

    @GetMapping("/selectMenuListByRoleId")
    public Result<List<Integer>> selectMenuListByRoleId(Integer roleId){
        return Result.success(systemMenuService.selectMenuListByRoleId(roleId));
    }

    @GetMapping("menuTreeList")
    public Result<List<MenuTree>> menuTreeList(){
        return Result.success(systemMenuService.selectMenuTreeList());
    }

    @GetMapping("selectById")
    public Result<SystemMenu> selectById(Integer id){
        return Result.success(systemMenuService.selectById(id));
    }

    @PostMapping
    public Result saveOrUpdate(@RequestBody @Validated SystemMenu systemMenu){
        systemMenuService.saveOrUpdate(systemMenu);
        return Result.success();
    }

    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable Integer id){
        SystemMenu systemMenu = systemMenuService.getById(id);
        if (systemMenu.getCreateType() == ResultCode.SUCCESS) {
            return Result.success(ResultCode.FAIL,"您不能删除系统内置菜单");
        }
        systemMenuService.removeById(id);
        return Result.success(ResultCode.SUCCESS,"删除成功");
    }
}
